//
//  HMusicSliderCell.m
//  HMusicPlayer
//
//  Created by Hament on 14/9/9.
//  Copyright (c) 2014年 HJ. All rights reserved.
//

#import "HMusicSliderCell.h"
#import "HMusicSlider.h"
#import "HView.h"
#import "HCoreConfig.h"
#import "HskinManager.h"
#import "HMainWC.h"
#define sliderThin  2  //slider 厚度

@implementation HMusicSliderCell


#pragma mark ---lazylaoding-----



#pragma mark ---custom method-----

-(void)initKnobViewWithRect:(NSRect)knobRect{


    if (!_knobView) {
        self.knobView=[[HView alloc] initWithFrame:knobRect];
        
        __weak HMusicSliderCell * weakself=self;
         self.knobView.fillBackGround=^(NSRect dirtyRect){
            
//            NSBezierPath * ovalPth=[NSBezierPath bezierPathWithOvalInRect:NSMakeRect(1, 1, 10, 10)];
//            NSColor * fcolor=[NSColor grayColor];//[NSColor colorWithCalibratedRed:1.0 green:0.0 blue:0.0 alpha:1.0];
//            
//            [fcolor setFill];
//            [ovalPth fill];
             NSImage * knobImg=nil;
             if (weakself.isVertical) {
                 knobImg=[[HCoreConfig shareConfig] getImageWithMainBundle:@"icon_slider_v"];
             } else {
                //knobImg=[NSImage imageNamed:@"icon_slider"];
                knobImg=[[HCoreConfig shareConfig] getImageWithMainBundle:@"icon_slider"];
             }
             
             [knobImg drawInRect:dirtyRect];
             
             
            
            
            
        };
        
        
        //self.knobImgView=[[NSImageView alloc] initWithFrame:<#(NSRect)#>]
        
        
    }

}


-(void)initSliderBottomBarViewWithRect:(NSRect)rect{
    
    if (!_sliderBottomBarView) {

        NSRect therect;
        
        if (self.isVertical) {
            
            therect=NSMakeRect(ceilf(rect.origin.x), rect.origin.y, sliderThin, rect.size.height);

        } else {
            
            therect=NSMakeRect(rect.origin.x, rect.origin.y, rect.size.width, sliderThin);

        }
        
        
        self.sliderBottomBarView=[[HView alloc] initWithFrame:therect];
        
        __weak HMusicSliderCell * ws=self;
        self.sliderBottomBarView.fillBackGround=^(NSRect dirtyRect){
            CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];

            
            //垂直布局的slider,坐标系有变化
            if (ws.isVertical) {
                [[HCoreConfig shareConfig].currentSkinManager.sliderHighlightColor setFill];
                
            } else {
              [[HCoreConfig shareConfig].currentSkinManager.sliderNormalColor setFill];
            }
            
            
            CGContextFillRect(context, NSRectToCGRect(dirtyRect));
        };
    }

}


-(void)initSliderHighlightBarViewWithRect:(NSRect)rect{
    
    
    if (!_sliderHighlightBarView) {
        
        
         NSRect therect;
        
        if (self.isVertical) {
            
            therect=NSMakeRect(ceilf(rect.origin.x), rect.origin.y, sliderThin, rect.size.height);
            
        } else {
            
            therect=NSMakeRect(rect.origin.x, rect.origin.y, rect.size.width, sliderThin);
            
        }
        
        
        self.sliderHighlightBarView=[[HView alloc] initWithFrame:therect];
        __weak HMusicSliderCell * ws=self;
        self.sliderHighlightBarView.fillBackGround=^(NSRect dirtyRect){
            CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
            //垂直布局的slider,坐标系有变化
            if (ws.isVertical) {
                [[HCoreConfig shareConfig].currentSkinManager.sliderNormalColor setFill];
            } else {
                [[HCoreConfig shareConfig].currentSkinManager.sliderHighlightColor  setFill];
            }
            CGContextFillRect(context, NSRectToCGRect(dirtyRect));
        };
    }

}

#pragma mark ---override-----

-(void)drawKnob:(NSRect)knobRect{
    

    ////[super drawKnob:knobRect];

//    if (self.isVertical) {
//     debugLog(@"V: knob rect:%@,x:%f",NSStringFromRect(knobRect),knobRect.origin.x);
//    } else {
//     debugLog(@"H: nob rect:%@,x:%f",NSStringFromRect(knobRect),knobRect.origin.x);
//    
//    }

    if (!_knobView) {
        //[self initKnobViewWithRect:knobRect];
        return;
    }
    
    
    
    if (self.isVertical) {
        self.knobView.frame=NSMakeRect(1, knobRect.origin.y, knobRect.size.width, knobRect.size.height);
    } else {
        self.knobView.frame=NSMakeRect(knobRect.origin.x, 0, knobRect.size.width, knobRect.size.height);
        
    }

}



- (void)drawBarInside:(NSRect)rect flipped:(BOOL)flipped
{
 
    //[super drawBarInside:rect flipped:flipped];
    
//    if (self.isVertical) {
//        debugLog(@"V:slider inner rect:%@,x:%f",NSStringFromRect(rect),rect.origin.x);
//        debugLog(@"V:self.trackRect:%@",NSStringFromRect(self.trackRect));
//    }
//    
//    else {
//
//         debugLog(@"H:slider inner rect:%@,x:%f",NSStringFromRect(rect),rect.origin.x);
//         debugLog(@"H:self.trackRect:%@",NSStringFromRect(self.trackRect));
//    }
    
   
    

    //外层  self.trackRect

    //内层 rect
    
    
    if (!_sliderBottomBarView) {
        [self.controlView lockFocus];
        [self initSliderBottomBarViewWithRect:rect];
        [self.controlView addSubview:self.sliderBottomBarView];
        [self.controlView unlockFocus];
    }
    
    if (!_sliderHighlightBarView) {
        [self.controlView lockFocus];
        [self initSliderHighlightBarViewWithRect:rect];
        [self.controlView addSubview:self.sliderHighlightBarView];
        [self.controlView unlockFocus];
    }
    
    
    if (!_knobView) {
        CGFloat knobHeigh=0.0;
        
        if (self.isVertical) {
            knobHeigh=self.trackRect.size.width-rect.origin.x*2;
        } else {
            knobHeigh=self.trackRect.size.height-rect.origin.y*2;
            
        }
        [self.controlView lockFocus];
        [self initKnobViewWithRect:NSMakeRect(0, 0, knobHeigh, knobHeigh)];
        [self.controlView addSubview:self.knobView];
        [self.controlView unlockFocus];

    }
    
    //垂直布局的slider,坐标系为左上角为0,0,不是左下角
    if (self.isVertical) {
        
        //在retina 与非retina之间拖放
//        self.sliderHighlightBarView.frame=CGRectMake(ceilf(rect.origin.x), rect.origin.y,ceilf(rect.size.width),(1.0-self.doubleValue/self.maxValue) * (rect.size.height));
        
        self.sliderHighlightBarView.frame=CGRectMake(ceilf(rect.origin.x), rect.origin.y,sliderThin,(1.0-self.doubleValue/self.maxValue) * (rect.size.height));
        
        self.sliderBottomBarView.frame=NSMakeRect(ceilf(rect.origin.x), rect.origin.y, sliderThin, rect.size.height);
         
    } else {
        // debugLog(@"H:rect:%@",NSStringFromRect(rect));
//        self.sliderHighlightBarView.frame=CGRectMake(rect.origin.x, rect.origin.y, self.doubleValue/self.maxValue * (rect.size.width), rect.size.height);
        
    
      self.sliderHighlightBarView.frame=CGRectMake(rect.origin.x, rect.origin.y, self.doubleValue/self.maxValue * (rect.size.width), sliderThin);
        
      self.sliderBottomBarView.frame=NSMakeRect(rect.origin.x, rect.origin.y, rect.size.width, sliderThin);
        
      
    }

    
    // NSLog(@"self.trackRect:%@, inner rect:%@",NSStringFromRect(self.trackRect),NSStringFromRect(rect));
    

    
}





-(void)stopTracking:(NSPoint)lastPoint at:(NSPoint)stopPoint inView:(NSView *)controlView mouseIsUp:(BOOL)flag {

    if (flag == YES) {
        if (! NSPointInRect(stopPoint, [self.controlView bounds]))
        {
        //[self.knobView setHidden:YES];
        }
        
        
    }
    
    
    if (self.isForDuration==YES) {
    //由于主线程阻塞timer的线程
    [HMainWC shareWc].refreshTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                         target:[HMainWC shareWc]
                                                       selector:@selector(refreshDurationUI)
                                                       userInfo:nil
                                                        repeats:YES];
        
       // [[HMainWC shareWc] refreshDurationUI];
//        debugLog(@"value:%f",[[HMainWC shareWc].durationSlider floatValue]);
//        
        [[HMainWC shareWc] changeCurrentTimeWithDurationSliderFloatValue:self.floatValue];
        
        }
}


- (BOOL)startTrackingAt:(NSPoint)startPoint inView:(NSView *)controlView{
    
    
if (self.isForDuration==YES) {
    [[HMainWC shareWc].refreshTimer invalidate];
}
    //debugLog(@"dsjkdsjd");
    
return     [super startTrackingAt:startPoint inView:controlView];

}

//- (BOOL)continueTracking:(NSPoint)lastPoint at:(NSPoint)currentPoint inView:(NSView *)controlView{
//
//
//self.isTracking=YES;
//return     [super continueTracking:lastPoint at:currentPoint inView:controlView];
//}


@end
